package org.nerve.android.ui.dialog;

import java.util.ArrayList;
import java.util.HashMap;

import org.nerve.android.ui.dialog.DialogHelper.MenuData;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.nerve.android.cloudoffice.R;

public class MenuDialog{
	/**菜单项的布局*/
	public static final int ITEM_ID = R.layout.menu_grid_item;
	public static final int GRID_ID = R.id.grid_menu;
	
	protected Context context;
	public boolean hasMenu = false;
	private Dialog dialog;
	
	/**对话框的id*/
	private int id;
	
	public MenuDialog(Context context, int id) {
		this.context = context;
		this.id = id;
	}
	
	/**
	 * @方法名称 :creatDialog
	 * @功能描述 :创建一个菜单Dialog，需要有数据才得，否则 menuDialog 为null
	 * @param menuDialog
	 * @param data
	 * @return :void
	 */
	public void creatDialog(final MenuData data){
		if(data == null)
			return ;
		if(data.icons.length > 0){
			View view = View.inflate(context, getMenuLayout(), null);
			
			//优先使用图片作为背景
			if(data.bgResource != -1){
				view.setBackgroundDrawable(
						getRepeatBG(context,data.bgResource)
						);
			}else if(data.bgColor != -1){
				Log.i("CellNote", "set bgColor---------");
				view.setBackgroundColor(data.bgColor);
			}
			
			GridView gView = (GridView)view.findViewById(GRID_ID);
			gView.setAdapter(getMenuAdapter(data));
			
			dialog = new Dialog(context);
			dialog.show();
			Window win = dialog.getWindow();
			//将dialog的背景透明化
			win.setBackgroundDrawable(new ColorDrawable(0));
			win.setGravity(getGravity());
			win.setContentView(view);
			
			if(data.listener != null){
				//注册菜单事件
				gView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						data.listener.onMenuClick(id,arg2);
						if(data.onlyOneTime)
							dialog.dismiss();
					}
				});
			}
			
			registerKey();
			
			this.hasMenu = true;
		}
	}
	
	/**
	 * @方法名称 :show
	 * @功能描述 :显示对话框
	 * @return :void
	 */
	public void show(){
		dialog.show();
	}
	
	protected SimpleAdapter getMenuAdapter(MenuData menuData) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuData.icons.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", menuData.icons[i]);
			map.put("itemText", menuData.labels[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(
				context, 
				data, 
				ITEM_ID,
				new String[] { "itemImage", "itemText" }, 
				new int[] {R.id.item_image, R.id.item_text }
				);
		return simperAdapter;
	}
	
	
	/*
	 * =================================================================
	 * 以下的方法可以在子类中重写，达到不同的效果
	 * =================================================================
	 */
	
	/**
	 * @方法名称 :getMenuLayout
	 * @功能描述 :主视图的引用
	 * @return
	 * @return :int
	 */
	protected int getMenuLayout(){
		return R.layout.menu_grid;
	}
	/**
	 * @方法名称 :registerKey
	 * @功能描述 :注册撤销菜单的热键，默认返回键，可以添加相应的按键。
	 * 				menuDialog，默认添加一个menu键的返回。
	 * @return :void
	 */
	protected void registerKey(){
		dialog.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键
					dialog.dismiss();
				return false;
			}
		});
	}
	/**
	 * @方法名称 :getGravity
	 * @功能描述 :设置菜单显示的位置
	 * @return
	 * @return :int
	 */
	protected int getGravity(){
		return Gravity.BOTTOM;
	}
	
	/**
	 * @方法名称 :getRepeatBG
	 * @功能描述 :得到一个repeat的背景
	 * @param id
	 * @return
	 * @return :Drawable
	 */
	public static Drawable getRepeatBG(Context cx, int id){
		BitmapDrawable bd = (BitmapDrawable)cx.getResources().getDrawable(id);
		bd.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		return bd;
	}
}